package com.macro.mall.portal.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;

public class MyDateTimeUtil {

	/**
	 * 时间格式simpleDateFormat1：{@code yyyy-MM-dd HH:mm:ss}
	 */
	public static SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

	/**
	 * 时间格式simpleDateFormat2：{@code yyyy/MM/dd}
	 */
	public static SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyy/MM/dd");

	/**
	 * 时间格式simpleDateFormat3：{@code yyyyMMddHHmmss}
	 */
	public static SimpleDateFormat simpleDateFormat3 = new SimpleDateFormat("yyyyMMddHHmmss");

	/**
	 * 时间格式simpleDateFormat3：{@code yyyyMMddHHmmss}
	 */
	public static SimpleDateFormat simpleDateFormat4 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");

	/**
	 * 格式化日期时间，格式：yyyy-MM-dd HH:mm:ss
	 * @param source
	 * @return
	 */
	public static String getDateFormat1(LocalDateTime source) {
		String timestamp = null;
		if (source != null) {
			timestamp = source.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
		}
		return timestamp;
	}

	/**
	 * 获取当前时间戳，格式：yyyy-MM-dd HH:mm:ss.SSS
	 * @return
	 */
	public static String getDateFormat4() {
		return simpleDateFormat4.format(System.currentTimeMillis());
	}

	/**
	 * 获取时间戳
	 * 
	 * @param source
	 *                   日期格式：yyyy-MM-dd HH:mm:ss
	 * @return
	 */
	public static Long getTimestamp(String source) {
		Date date;
		try {
			date = simpleDateFormat1.parse(source);
		} catch (ParseException e) {
			return 0L;
		}
		return date.getTime();
	}

	/**
	 * 获取日期字符串，格式：yyyy/MM/dd
	 * 
	 * @param timestamp
	 *                      13位时间戳
	 * @return
	 */
	public static String getSimpleDateFormat2(Long timestamp) {
		String dateStr = simpleDateFormat2.format(new Date(timestamp));
		return dateStr;
	}

	/**
	 * 获取日期时间字符串，格式：yyyyMMddHHmmss
	 * 
	 * @param timestamp
	 * @return
	 */
	public static String getSimpleDateFormat3(Long timestamp) {
		String dateStr = simpleDateFormat3.format(new Date(timestamp));
		return dateStr;
	}

	/**
	 * 获取日期时间字符串，格式：yyyyMMddHHmmss
	 *
	 * @return
	 */
	public static String getSimpleDateFormat3() {
		String dateStr = simpleDateFormat3.format(new Date());
		return dateStr;
	}

	/**
	 * 将13位时间戳格式化时间为：xx秒前，xx分前，xx小时前……
	 * 
	 * @param timestemp
	 * @return
	 */
	public static String timeAgo(long timestemp) {
		String shortstring = null;
		long now = Calendar.getInstance().getTimeInMillis();
		long updateAt = timestemp;
		long deltime = (now - updateAt) / 1000;
		if (deltime > 365 * 24 * 60 * 60) {
			shortstring = (int) (deltime / (365 * 24 * 60 * 60)) + "年前";
		} else if (deltime > 24 * 60 * 60) {
			shortstring = (int) (deltime / (24 * 60 * 60)) + "天前";
		} else if (deltime > 60 * 60) {
			shortstring = (int) (deltime / (60 * 60)) + "小时前";
		} else if (deltime > 60) {
			shortstring = (int) (deltime / (60)) + "分钟前";
		} else if (deltime > 1) {
			shortstring = deltime + "秒前";
		} else {
			shortstring = "1秒前";
		}
		return shortstring;
	}

	/**
	 * 将yyyy-MM-dd HH:mm:ss格式时间转换为：xx秒前，xx分前，xx小时前……
	 * 
	 * @param
	 * @return
	 */
	public static String timeAgo(String source) {
		long timestemp = getTimestamp(source);
		String shortstring = null;
		long now = Calendar.getInstance().getTimeInMillis();
		long updateAt = timestemp;
		long deltime = (now - updateAt) / 1000;
		if (deltime > 365 * 24 * 60 * 60) {
			shortstring = (int) (deltime / (365 * 24 * 60 * 60)) + "年前";
		} else if (deltime > 24 * 60 * 60) {
			shortstring = (int) (deltime / (24 * 60 * 60)) + "天前";
		} else if (deltime > 60 * 60) {
			shortstring = (int) (deltime / (60 * 60)) + "小时前";
		} else if (deltime > 60) {
			shortstring = (int) (deltime / (60)) + "分钟前";
		} else if (deltime > 1) {
			shortstring = deltime + "秒前";
		} else {
			shortstring = "1秒前";
		}
		return shortstring;
	}
}
